package com.ls.controller;

import com.ls.entity.ResResult;
import com.ls.entity.UsersVO;
import com.ls.service.UserService;
import com.ramostear.captcha.HappyCaptcha;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class PageController {
    @Autowired
    private UserService userService;


    @GetMapping("/login")
    public String login() {
        return "login";
    }

    //登录
    @PostMapping("/api/user/login")
    @ResponseBody
    public ResResult login(@RequestBody UsersVO user, HttpServletRequest request){
        boolean flag = HappyCaptcha.verification(request,user.getCaptcha(),true);
        if(flag){
            //判断用户名和密码是否一致
            return  userService.login(user);
        }else{
            return ResResult.error("验证码输入错误");
        }
    }

    @GetMapping("/index")
    @ApiOperation("首页")
    public String index() {
        return "index";
    }

    @GetMapping("/welcome")
    @ApiOperation("欢迎页")
    public String welcome() {
        return "welcome";
    }


    @GetMapping("/captcha")
    @ApiOperation("验证码")
    public void happyCaptcha(HttpServletRequest request, HttpServletResponse response){
        HappyCaptcha.require(request,response).build().finish();
    }

}
